import { _decorator, BoxCollider, Component, instantiate, Node, Prefab } from 'cc';
import { PrefsManager } from './PrefsManager';
import { PARTGRADE, PartPos } from './GameConstant';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { WeaponPart } from './WeaponPart';
import { BoxController } from './BoxController';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
const { ccclass, property } = _decorator;

@ccclass('BoxManager')
export class BoxManager extends Component {
    public static Instance: BoxManager = null;

    @property(Node)
    Boxs: Node[] = [];

    @property(Node)
    PartsParent: Node = null;

    protected onLoad(): void {
        BoxManager.Instance = this;
    }

    protected start(): void {
        this.intParts();
    }

    intParts() {
        const parts = PrefsManager.Instance.userData.Parts;
        for (let i = 0; i < parts.length; i++) {
            if (parts[i] == PARTGRADE.GRADE0) continue;
            const path: string = "Parts/Part" + parts[i];
            BundleManager.LoadPrefab(Bundles.Prefabs, path).then((prefab: Prefab) => {
                const part: Node = instantiate(prefab);
                part.parent = this.PartsParent;
                part.getComponent(WeaponPart).add(this.Boxs[i]);
                part.setPosition(PartPos[this.Boxs[i].name])
            })
        }
    }

    upgradePart(nextPart: PARTGRADE, box: Node) {
        const path: string = "Parts/Part" + nextPart;
        BundleManager.LoadPrefab(Bundles.Prefabs, path).then((prefab: Prefab) => {
            if (!prefab) return;
            const part: Node = instantiate(prefab);
            part.parent = this.PartsParent;
            part.getComponent(WeaponPart).add(box);
            part.setPosition(PartPos[box.name])
            BoxManager.Instance.save();
            if (nextPart == PARTGRADE.GRADE5) {
                part.getComponent(WeaponPart).IsMax = true;
                return;
            }
        })
    }

    getBox(): Node | null {
        for (let index = 0; index < this.Boxs.length; index++) {
            if (this.Boxs[index].getComponent(BoxController).IsNull) {
                return this.Boxs[index];
            }
        }
        return null;
    }

    addNewPiece() {
        const name = this.getBox().name;

        const path: string = "Parts/Part1";
        BundleManager.LoadPrefab(Bundles.Prefabs, path).then((prefab: Prefab) => {
            const part: Node = instantiate(prefab);
            part.parent = this.PartsParent;
            part.getComponent(WeaponPart).add(this.getBox());
            part.setPosition(PartPos[name])
            this.save();
        })
    }

    save() {
        const parts = PrefsManager.Instance.userData.Parts;
        for (let index = 0; index < this.Boxs.length; index++) {
            const element = this.Boxs[index].getComponent(BoxController);
            parts[index] = element.WeaponPart ? element.WeaponPart.getComponent(WeaponPart).PartType : PARTGRADE.GRADE0;
        }
        PrefsManager.Instance.saveData();
    }

}


